package leetcode_day._2022._202202._2128;

/**
 * @author yzh
 * @version 1.0
 * @date 2022/2/24 20:29
 * 球会落何处
 * 算法：模拟
 */
public class _24_1706 {

    int m, n;
    int[][] g;
    public int[] findBall(int[][] grid) {
        g = grid;
        m = grid.length;
        n = grid[0].length;
        int[] ans = new int[n];
        for (int i = 0; i < n; i++) ans[i] = getVal(i);
        return ans;
    }
    int getVal(int i) {
        int r = 0, c = i;
        while (r < m) {
            // ne ☞ next
            int ne = c + g[r][c];
            // 是否越界
            if (ne < 0 || ne >= n) return -1;
            // 是否形成 V 字型
            if (g[r][c] != g[r][ne]) return -1;
            r++;
            c = ne;
        }
        return c;
    }

}
